/**
 * 
 */
package co.fitstart.api.handler.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.support.Result;
import com.demo2do.core.support.sms.handler.SmsHandler;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;

import co.fitstart.api.entity.MessageType;
import co.fitstart.api.handler.SmsSender;

/**
 * @author downpour
 *
 */
@Component("smsSender")
public class SmsSenderImpl implements SmsSender {
    
    private static final Logger logger = LoggerFactory.getLogger(SmsSender.class);
    
    @Autowired
    private SmsHandler smsHandler;

    @Value("#{config['sms.company']}")
    private String company;

    /*
     * (non-Javadoc)
     * @see co.fitstart.api.handler.SmsSender#sendBuyCourseNotice(java.lang.String, java.lang.String, java.lang.String)
     */
    public void sendBuyCourseNotice(String user, String mobile, String product) {
        Result result = new Result();
        
        // Get message template
        String template = smsHandler.getShortMessageTemplate(MessageType.NEWORDER_NOTICE);
        
        if (template != null) {
            
            String today = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
            
            Map<String, String> values = new HashMap<String, String>(2);
            values.put("company", company);
            values.put("user", user);
            values.put("time", today);
            values.put("course", product);
            
            String message = StringUtils.replacePlaceHolder(template, values, "#", "#");
            
            // Send message
            result = smsHandler.sendShortMessage(mobile, message);
            
            if(!result.isValid()) {
                logger.error("sendBuyCourseNotice() error", JsonUtils.toJsonString(result));
            }
        }
        
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.api.handler.SmsSender#sendSaleOtherProductNotice(java.lang.String, java.lang.String)
     */
    public void sendSaleProductNotice(String mobile, String type) {
    	Result result = new Result();
        
        // Get message template
        String template = smsHandler.getShortMessageTemplate(MessageType.PRODUCT_SALE_NOTICE);
        
        if (template != null) {
            
            Map<String, String> values = new HashMap<String, String>(4);
            values.put("company", company);
            values.put("type", type);
            
            String message = StringUtils.replacePlaceHolder(template, values, "#", "#");
            
            // Send message
            result = smsHandler.sendShortMessage(mobile, message);
            
            if(!result.isValid()) {
                logger.error("sendSaleOtherProductNotice() error", JsonUtils.toJsonString(result));
            }
        }
    }

}
